import 'package:flutter/material.dart';
import 'package:osc/common/MyTextStyle.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/widgets/common_scaffold.dart';
import 'package:osc/ui/widgets/small_widgets.dart';
import 'package:osc/utils/pd_util.dart';
import 'package:osc/utils/string_util.dart';

/// 编辑问答
class PublishQuestion extends StatefulWidget {
  @override
  _PublishQuestionState createState() => _PublishQuestionState();
}

class _PublishQuestionState extends State<PublishQuestion> {

  TextEditingController contentEditingController;
  TextEditingController titleEditingController;
  FocusNode focusNodeContent;
  FocusNode focusNodeTitle;

  @override
  void initState() {
    super.initState();
    contentEditingController = TextEditingController();
    titleEditingController = TextEditingController();
    focusNodeContent = FocusNode();
    focusNodeTitle = FocusNode();
  }

  @override
  void dispose() {
    super.dispose();
    titleEditingController.dispose();
    contentEditingController.dispose();
    focusNodeTitle.dispose();
    focusNodeContent.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CommonScaffold(
        title: "编辑问答",
        actions: <Widget>[
          SmallWidgets.buildAction(
            data: "发送",
            onTap: (){
              checkInput();
            },
          ),
        ],
        body: ListView(
          padding: EdgeInsets.symmetric(horizontal: Dimens.gap_activity_vertical),
          children: <Widget>[
            buildTextField("请输入标题", focusNodeTitle, titleEditingController, MyTextStyle.QUESTION_INPUT_TITLE, MyTextStyle.QUESTION_INPUT_TITLE_HINT),
            SmallWidgets.buildLine(),
            buildTextField("请输入内容", focusNodeContent, contentEditingController, MyTextStyle.QUESTION_INPUT_CONTENT, MyTextStyle.QUESTION_INPUT_CONTENT_HINT),
          ],
        ),
    );
  }

  buildTextField(String hint, FocusNode focusNode, TextEditingController controller, TextStyle textStyle, TextStyle hintStyle, {TextInputType keyboardType, int maxLines}){
    return TextField(
      style: textStyle,
      maxLines: maxLines,
      focusNode: focusNode,
      controller: controller,
      keyboardType: keyboardType,
      decoration: InputDecoration(
        hintText: hint,
        hintStyle: hintStyle,
        border: InputBorder.none,
        focusedBorder: InputBorder.none,
        errorBorder: InputBorder.none,
        focusedErrorBorder: InputBorder.none,
        disabledBorder: InputBorder.none,
        enabledBorder: InputBorder.none,
      ),
    );
  }


  checkInput(){
    if(StringUtil.isEmpty(titleEditingController.text)){
      showTips("请输入标题");
      FocusScope.of(context).requestFocus(focusNodeTitle);
    }
    else if(StringUtil.isEmpty(contentEditingController.text)){
      showTips("请输入问答内容");
      FocusScope.of(context).requestFocus(focusNodeContent);
    } else {

    }
  }

  showTips(String msg){
    PdUtil.showToast(msg: msg);
  }
}
